package com.cincc.cincc_pbx.adapter;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cincc_pbx.R;
import com.cincc.cincc_pbx.model.NavModel;

import java.util.List;

public class NavAdapter extends RecyclerView.Adapter<NavAdapter.MyViewHolder> {

    private Context context;
    private List<NavModel> list;

    public NavAdapter(Context context, List<NavModel> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.nav_item, parent, false);
        return new MyViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        NavModel item = list.get(position);

        holder.nameText.setText(item.getTitle());
        holder.itemView.setOnClickListener(v -> {
            iOnClickItem.OnClick(position, item);
        });

        if (position == list.size() - 1) {
            holder.rightImg.setVisibility(View.GONE);
            holder.nameText.setTextColor(context.getColor(R.color.black));
        } else {
            holder.nameText.setTextColor(context.getColor(R.color.bottom_select));
            holder.rightImg.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface IOnClickItem {
        void OnClick(int position, NavModel item);
    }

    private IOnClickItem iOnClickItem;

    public void setOnClickItem(IOnClickItem iOnClickItem) {
        this.iOnClickItem = iOnClickItem;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        ImageView rightImg;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nameText = itemView.findViewById(R.id.title_tv);
            rightImg = itemView.findViewById(R.id.right_arrow_img);
        }
    }
}
